<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">

</style>
<!-- 手机端能显示菜单 -->
<style>
		@media screen and (max-width: 420px) {
			.page-header {
				display: none;
			}
			.main > a.btn {
				display: none;
			}
			#dynamic-table {
				width: auto !important;
			}
			/* 隐藏功能模块，自动更新，考务模块，班级主题，过期时间，创建时间 */
			#dynamic-table th:nth-child(3),th:nth-child(4),th:nth-child(5),th:nth-child(6),th:nth-child(7),th:nth-child(8),th:nth-child(9) {
				display: none;
			}
			#dynamic-table td:nth-child(3),td:nth-child(4),td:nth-child(5),td:nth-child(6),td:nth-child(7),td:nth-child(8),td:nth-child(9) {
				display: none;
			}
			/* 隐藏校区管理 */
			.li-campus {
				display: none;
			}
			/* 登录账号管理弹窗 */
			#modal-table {
				font-size: 1.1rem;
			}
			/* 隐藏删除用户按钮 */
			.delete-user {
				display: none;
			}
		}
	</style>
<div class="page-header">
	<h1>学校管理</h1>
</div>

<div class="col-xs-12 main">
	<a href="<?=admin_url('admin/school_edit')?>" class="btn btn-primary">
		<i class="ace-icon fa fa-plus"></i> 新建学校
	</a>
	<!-- <a href="<?=admin_url('admin/school_edit?from='. X_DATA_HAOZHUANYE)?>" class="btn btn-success ml10">
		<i class="ace-icon fa fa-link"></i> 导入学校
	</a>
	<a href="<?=admin_url('admin/school_edit?from='. X_DATA_CHAOXING)?>" class="btn btn-success ml10">
		<i class="ace-icon fa fa-link"></i> 导入超星学校
	</a>
	<a href="<?=admin_url('admin/school_edit?from='. X_DATA_YINTONG)?>" class="btn btn-success ml10">
		<i class="ace-icon fa fa-link"></i> 导入银通学校
	</a>
	<a href="<?=admin_url('admin/school_edit?from='. X_DATA_DIANXIN)?>" class="btn btn-success ml10">
		<i class="ace-icon fa fa-link"></i> 导入电信学校
	</a>
	<a href="<?=admin_url('admin/school_edit?from='. X_DATA_FEICHEN)?>" class="btn btn-success ml10">
		<i class="ace-icon fa fa-link"></i> 导入斐宸学校
	</a> -->
	<!-- 筛选 -->
	<div class="widget-body">
		<div class="widget-main" style="padding: 25px 0 0 0;">
			<div class="form-inline keyword-search-div" style="margin-bottom: 2px;">
				<!-- <label class="keyword-search-label">名称&nbsp;&nbsp;&nbsp;&nbsp;</label> -->
				<div class="keyword-search-box" style="margin-left: 0;">
					<div class="div-search">
						<input style="float:left;height:30px;width:180px;border:1px solid #669FC7;padding:0 30px 0 14px;border-radius:10px 0 0 10px !important;-moz-border-radius:10px 0 0 10px;" type="text" name="keyword" placeholder="搜索学校名称..." autocomplete="off">
						<span id="empty-search" style="position:relative;right:110px;cursor:pointer;display:none;">×</span>
						<button id="btn-search" class="btn-search btn-info" style="outline:none;float:left;height:30px;width:44px;border:1px solid #669FC7;border:0;border-radius:0 10px 10px 0;-moz-border-radius:0 10px 10px 0;margin-right:40px;">
							<i class="ace-icon fa fa-search nav-search-icon white"></i>
						</button>
					</div>
				</div>
			</div>
		</div>
	</div>

			
	<!-- 学校表格 -->
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="">
		<thead>
			<tr>
				<th>学校ID</th>
				<th>学校名称</th>
				<th style="width: 30%;">功能模块</th>
				<th>人脸识别</th>
				<th>自动更新</th>
				<!-- <th>招考查询</th> -->
				<th>考务模块</th>
				<!-- <th>智学网</th> -->
				<th style="white-space: nowrap;">班牌主题</th>
				<th>过期时间</th>
				<th>创建时间</th>
				<th></th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

<!-- 表格弹窗 -->
<div id="modal-table" class="modal fade" tabindex="-1">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button> <span id="school-name"></span>
					学校登录账号管理
				</div>
			</div>
			<div class="modal-body no-padding">
				<table class="table table-striped table-bordered table-hover no-margin-bottom no-border-top">
					<thead>
						<tr>
							<th>登录账号</th>
							<th>密码管理</th>
							<th>账号管理</th>
						</tr>
					</thead>
					<tbody></tbody>
				</table>
			</div>
			<div class="modal-footer no-margin-top">
				<a href="#modal-account" class="btn btn-sm btn-primary pull-left" data-toggle="modal">
					<i class="ace-icon fa fa-plus"></i> 新增账号
				</a>
			</div>
		</div>
	</div>
</div>
<!-- 新增账号弹窗 -->
<div id="modal-account" class="modal fade" tabindex="-1">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button> <span id="school-name"></span>
					新增登录账号
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10" id="account-form">
							<input type="hidden" name="id" />
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">登录账号：</label>
								<div class="col-sm-6">
									<input class="form-control" type="text" name="username" placeholder="请填写学校管理员登录账号"/>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">登录密码：</label>
								<div class="col-sm-6">
									<input class="form-control" type="text" name="password" placeholder="请填写学校管理员登录密码"/>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-account">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>
<!-- 修改密码弹窗 -->
<div id="modal-edit-password" class="modal fade" tabindex="-1">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button> <span id="school-name"></span>
					修改账号密码
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10" id="edit-password-form">
							<input type="hidden" name="id" />
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">登录账号：</label>
								<div class="col-sm-6">
									<input class="form-control" type="text" name="username" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">登录密码：</label>
								<div class="col-sm-6">
									<input class="form-control" type="text" name="password" placeholder="请填写学校管理员登录密码"/>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="edit-password-action">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>
<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	var schoolId = 0;

	$(function(){
		var table = $('#dynamic-table').dt({
			"ajaxUrl": '<?=admin_url('admin/get_school_list')?>',
			"columns": [
				{"data": "id"},
				{"data": function(ret){
					return ret.schoolName + '<span class="label label-success ml10">'+ ret.schoolFromName +'</span>';
				}},
				{"data": function(ret){
					var moduleHtml = "";
					for(var i in ret.module) moduleHtml += '<span class="label label-info mr10">'+ ret.module[i] +'</span>';
					return moduleHtml;
				}},
				{"data": function(ret){
					return ret.faceStyle;
				}},
				{"data": function(ret){
					return ret.school_brand_update == 1 ? '<span class="label label-success ml10">开启</span>' : '<span class="label label-danger ml10">关闭</span>';
				}},
				// {"data": function(ret){
				// 	return ret.school_exam_query == 1 ? '<span class="label label-success ml10">开启</span>' : '<span class="label label-danger ml10">关闭</span>';
				// }},
				{"data": function(ret){
					return ret.school_exam == 1 ? '<span class="label label-success ml10">开启</span>' : '<span class="label label-danger ml10">关闭</span>';
				}},
				// {"data": function(ret){
				// 	return ret.school_zhi_xue == 1 ? '<span class="label label-success ml10">开启</span>' : '<span class="label label-danger ml10">关闭</span>';
				// }},
				{"data": function(ret){
	            	return ret.themeStyle;
				}},
	            {"data": "expire_date"},
	            {"data": "createtime"},
	            {"data": function(ret){
	            	var action = "";
	            	if(ret.school_from && ret.school_from == "<?= X_DATA_HAOZHUANYE ?>") {
						action += '<li><a href="javascript:;" class="update-hzy-school" data-id="'+ ret.id +'"><i class="fa fa-refresh"></i> 同步数据</a></li>';
					} else if(ret.school_from && ret.school_from == "<?= X_DATA_YINTONG ?>") {
						action += '<li><a href="javascript:;" class="update-yt-school" data-id="'+ ret.id +'"><i class="fa fa-refresh"></i> 同步数据</a></li>';
					} else if(ret.school_from && ret.school_from == "<?= X_DATA_CHAOXING ?>") {
						action += '<li><a href="javascript:;" class="update-cx-school" data-id="'+ ret.id +'"><i class="fa fa-refresh"></i> 同步数据</a></li>';
					} else if(ret.school_from && ret.school_from == "<?= X_DATA_DIANXIN ?>") {
						action += '<li><a href="javascript:;" class="update-dx-school" data-id="'+ ret.id +'"><i class="fa fa-refresh"></i> 同步数据</a></li>';
					} else if(ret.school_from && ret.school_from == "<?= X_DATA_FEICHEN ?>") {
						action += '<li><a href="javascript:;" class="update-fc-school" data-id="'+ ret.id +'"><i class="fa fa-refresh"></i> 同步数据</a></li>';
					}

	            	return '<div class="btn-group">\
						<button data-toggle="dropdown" aria-expanded="false" class="btn btn-sm btn-success dropdown-toggle">\
							<i class="fa fa-gear"></i> 学校管理 <i class="ace-icon fa fa-angle-down icon-on-right"></i>\
						</button>\
						<ul class="dropdown-menu dropdown-danger">\
							<li>\
								<a href="<?=admin_url('admin/school_edit')?>/'+ ret.id +'"><i class="fa fa-edit"></i> 修改学校信息</a>\
							</li>'+ action +'\
							<li class="divider"></li>\
							<li>\
								<a href="#modal-table" class="manage-account" data-toggle="modal" data-id="'+ ret.id +'"><i class="fa fa-user"></i> 登录账号管理</a>\
							</li>\
							<li class="li-campus">\
								<a href="<?=admin_url('admin/campus')?>/'+ ret.id +'"><i class="fa fa-building"></i> 校区管理</a>\
							</li>\
							<li class="divider"></li>\
							<li>\
								<a href="<?=admin_url('admin/brand')?>/'+ ret.id +'"><i class="fa fa-table"></i> 班牌管理</a>\
							</li>\
							<li class="divider"></li>\
							<li>\
								<a href="<?=admin_url('admin/charge')?>/'+ ret.id +'"><i class="fa fa-rmb"></i> 收费设置</a>\
							</li>\
						</ul>\
					</div>';
	            }},
			]
		});
		// 敲回车就搜索
		$("[name=keyword]").keypress(function (e) {
                if (e.which == 13) {
                    $("#btn-search").trigger("click");
                }
		});
		// 清空搜索框
		$("[name=keyword]").keyup(function() {
			if($(this).val().length > 0) {
				$("#empty-search").show();
			} else {
				$("#empty-search").hide();
			}
		});
		$("#empty-search").click(function(){
			$("[name=keyword]").val('');
			$(this).hide();
			// 重置搜索结果
			searchData(table);
		})
		// 搜索标题
		$("#btn-search").click(function() {
			searchData(table);
		})

		// 账号表单
		var accountForm = $("#account-form");

		// 管理学校登录账号
		$("body").on("click", ".manage-account", function(){
			schoolId = $(this).data("id");
			accountForm.find("[name=id]").val(schoolId);
			// 获取登录账号列表
			get_school_user_list();
		})

		// 新增账号弹窗
		$("#save-account").click(function(){
			accountForm.submit();
		})
		// 新增登录账号
		var submitUrl = "<?=admin_url('admin/save_school_account_action')?>";
		form_submit(accountForm, submitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) {
					$("#modal-account").modal('hide');
					get_school_user_list();
				}
			});
		})

		// 重置登录密码
		$("body").on("click", ".reset-password", function(){
			var id = $(this).closest("tr").data("id");
			layer.confirm('确定将密码重置为“123456”吗？', function(index){
				layer.close(index);
				// 提交数据
				var actionUrl = "<?=admin_url('admin/reset_school_user_password')?>";
				var actionData = {
					"id": id
				};
				ajax_get(actionUrl, actionData);
			});
		})

		// 修改登录密码
		$("body").on("click", ".edit-password", function(){
			var id = $(this).data("id");
			var username = $(this).data("username");
			$("#modal-edit-password [name=id]").val(id);
			$("#modal-edit-password [name=username]").val(username);
		})
		// 保存密码
		$("body").on("click", "#edit-password-action", function(){
			var id = $("#modal-edit-password [name=id]").val();
			var password = $("#modal-edit-password [name=password]").val();
			if(password.trim() == '') {
				alert('密码不能为空');
				return;
			}
			// 提交数据
			var actionUrl = "<?=admin_url('admin/reset_school_user_password')?>";
			var actionData = {
				"id": id,
				password: password
			};
			ajax_get(actionUrl, actionData, function(res) {
				if(res.success) {
					$("#modal-edit-password").modal('hide');
				}
			});
		})

		// 删除用户
		$("body").on("click", ".delete-user", function(){
			var id = $(this).closest("tr").data("id");
			layer.confirm('确定删除该用户吗？', function(index){
				layer.close(index);

				// 提交数据
				var actionUrl = "<?=admin_url('admin/delete_school_user')?>";
				var actionData = {"id": id};
				ajax_get(actionUrl, actionData, function(){
					get_school_user_list();
				});
			});
		})
		// 一键登录
		$("body").on("click", ".one-click-login", function(){
			var id = $(this).closest("tr").data("id");
			var getUrl = "<?=admin_url('login/one_click_login_action')?>";
			ajax_get(getUrl, {user_id: id}, function(res){
				window.location.href = "<?=admin_url("home/index")?>";
			})
		})
		// 同步学校信息
		$("body").on("click", ".update-hzy-school", function(){
			var schoolId = $(this).data("id");
			layer.confirm('确定同步吗？', function(index){
				layer.close(index);
				// 执行同步
				var actionUrl = "<?=admin_url('hzy/update_hzy_school')?>/" + schoolId
				ajax_get(actionUrl, {}, function(res) {
					if(res.success) {
						// 插入日志
						logData = {
							'hideLoading': true,
							'school_id': schoolId
						};
						$logUrl = "<?=admin_url('admin/save_sync_log_action')?>";
						ajax_post($logUrl, logData);
					}
				});
			});
		});

		// 同步银通学校信息
		$("body").on("click", ".update-yt-school", function(){
			var schoolId = $(this).data("id");
			layer.confirm('确定同步吗？', function(index){
				layer.close(index);
				// 执行同步
				var actionUrl = "<?=admin_url('yt/update_yt_school')?>/" + schoolId
				ajax_get(actionUrl, {}, function(res) {
					if(res.success) {
						// 插入日志
						logData = {
							'hideLoading': true,
							'school_id': schoolId
						};
						$logUrl = "<?=admin_url('admin/save_sync_log_action')?>";
						ajax_post($logUrl, logData);
					}
				});
			});
		});
		
		// 同步超星学校信息
		$("body").on("click", ".update-cx-school", function(){
			var schoolId = $(this).data("id");
			layer.confirm('确定同步吗？', function(index){
				layer.close(index);
				// 执行同步
				var actionUrl = "<?=admin_url('cx/update_cx_school')?>/" + schoolId
				ajax_get(actionUrl, {}, function(res) {
					if(res.success) {
						// 插入日志
						logData = {
							'hideLoading': true,
							'school_id': schoolId
						};
						$logUrl = "<?=admin_url('admin/save_sync_log_action')?>";
						ajax_post($logUrl, logData);
					}
				});
			});
		});

		// 同步电信学校信息
		$("body").on("click", ".update-dx-school", function(){
			var schoolId = $(this).data("id");
			layer.confirm('确定同步吗？', function(index){
				layer.close(index);
				// 执行同步
				var actionUrl = "<?=admin_url('dx/update_dx_school')?>/" + schoolId
				ajax_get(actionUrl, {}, function(res) {
					if(res.success) {
						// 插入日志
						logData = {
							'hideLoading': true,
							'school_id': schoolId
						};
						$logUrl = "<?=admin_url('admin/save_sync_log_action')?>";
						ajax_post($logUrl, logData);
					}
				});
			});
		});

		// 同步斐宸学校信息
		$("body").on("click", ".update-fc-school", function(){
			var schoolId = $(this).data("id");
			layer.confirm('确定同步吗？', function(index){
				layer.close(index);
				// 执行同步
				var actionUrl = "<?=admin_url('fc/update_fc_school')?>/" + schoolId
				ajax_get(actionUrl, {}, function(res) {
					if(res.success) {
						// 插入日志
						logData = {
							'hideLoading': true,
							'school_id': schoolId
						};
						$logUrl = "<?=admin_url('admin/save_sync_log_action')?>";
						ajax_post($logUrl, logData);
					}
				});
			});
		});
	})

	// 获取登录账号列表
	function get_school_user_list(){
		var ajaxUrl = "<?=admin_url('admin/get_school_user_action')?>";
		ajax_get(ajaxUrl, {"id": schoolId}, function(res){
			var tbody = $("#modal-table tbody");
			var retData = res.data;
			// 显示账号列表
			tbody.empty();
			for(var i in retData) {
				tbody.append('<tr data-id="'+ retData[i].id +'">\
					<td>'+ retData[i].username +'</td>\
					<td><a href="javascript:;" class="reset-password"><i class="fa fa-refresh"></i> 重置密码</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\
					<a href="#modal-edit-password" class="edit-password" data-toggle="modal" data-id="' + retData[i].id + '" data-username="' + retData[i].username + '"><i class="fa fa-edit"></i> 修改密码</a></td>\
					<td><a href="javascript:;" class="delete-user red"><i class="fa fa-times"></i> 删除用户</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\
					<a href="javascript:;" class="one-click-login green"><i class="fa fa-sign-in"></i> 一键登录</a></td>\
				</tr>');
			}
		}, function(){
			window.location.reload();
		})
	}
	function searchData(table) {
			var keyword = $("[name=keyword]").val();
			table.setAjaxData({
				"keyword": keyword
			});
		}
</script>